﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Baidu.Aip.Ocr;
using Baidu.Aip.Demo;
namespace WinFormStudy
{
    public partial class Form_PicRec : System.Windows.Forms.Form
    {
        public Form_PicRec()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Show();
            Control.CheckForIllegalCrossThreadCalls = false;//这一行是关键

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cbType.SelectedIndex = 0;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择图片";
            fileDialog.Filter = "图片文件|*.jpg;*.png";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] files = fileDialog.FileNames;
                string sb="";
                if(files!=null&&files.Length>0)
                {
                    foreach(string f in files)
                    {
                        sb=sb+f+";";
                    }
                }
                if(sb.Length>0)
                {
                    
                    sb=sb.Substring(0,sb.Length-1);
                }
                tbFileName.Text = sb;
            }

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string recType = cbType.Text;
            if (tbFileName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请选择图片后再进行识别！");
                return;
            }
            tbResult.Text = "";
            switch (recType)
            {
                case "识别文字": RecWord();
                    break;
                case "识别身份证":
                    break;
                case "识别银行卡":
                    break;
                case "识别驾驶证":
                    break;
                case "识别营业执照":
                    break;

            }
        }

        public void RecWord()
        {
            Thread t = new Thread(RunThread);
            t.Start();

        }

        private void RunThread(object obj)
        {
            if (tbFileName.Text.Length > 0)
            {
                string[] fileNames = tbFileName.Text.Split(';');
                if (fileNames.Length > 0)
                {
                    lbStatus.Text = "识别中...";
                    lbStatus.ForeColor = Color.Red;
                    string word = "";
                    foreach (string f in fileNames)
                    {
                        word = word + OcrDemo.Accurate(f) + "\r\n\r\n";
                    }
                    
                    tbResult.Text = word;
                    lbStatus.Text = "识别完成";
                    lbStatus.ForeColor = Color.Green;
                }
            }
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbType.Text == "识别身份证")
            {
                lbTip.Visible = true;
            }
            else
            {
                lbTip.Visible = false;
            }
        }
    }
}
